import pako from "pako";

export function unzip(data: any) {
  const charData = window
    .atob(data)
    .split("")
    .map(function (x) {
      return x.charCodeAt(0);
    });
  const binData = new Uint8Array(charData);
  return pako.inflate(binData, { to: "string" });
}

export function zip(str: any) {
  const data: any = pako.gzip(str);
  const res = window.btoa(data);
  return res;
}

export function stringToContent(str: any) {
  if (str.startsWith('{"dpmx"')) {
    return JSON.parse(str);
  } else {
    const string = unzip(str);
    return JSON.parse(string);
  }
}

// eslint-disable-next-line no-unused-vars
function ab2str(buf: any) {
  let binaryString = "";
  const bytes = new Uint16Array(buf);
  const length = bytes.length;
  for (let i = 0; i < length; i++) {
    binaryString += String.fromCharCode(bytes[i]);
  }
  return binaryString;
}
